package com.yhb.channel.utils;

import com.yhb.channel.impl.YPL.YPLConstant;
import com.yhb.channel.impl.YPL.YPLException;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

public class ChannelStringUtils {
    private static SimpleDateFormat rawPlainDateFormat = new SimpleDateFormat("yyyyMMdd");

    /**
     * 将客户端传上来的身份证有效期转码
     * 客户端有效期格式如下
     * 1. 20190101-20190102
     * 2. 20190101-长期
     * @param validation 客户端有效期
     * @param dateFormat 格式
     * @return 有效期新格式数组 第一个是起始时间 第二个是终止时间
     */
    public static String[] convertIdCardValidation(String validation, String dateFormat){
        return convertIdCardValidation("20991231", validation, dateFormat);
    }
    public static String[] convertIdCardValidation(String foreverString, String validation, String dateFormat){
        SimpleDateFormat newDateFormat = new SimpleDateFormat(dateFormat);
        String FOREVER = "长期";

        String[] dateStrings = validation.split("-");
        String begin = dateStrings[0];
        String end = dateStrings[1];

        try {
            Date beginDate = rawPlainDateFormat.parse(begin);
            Date endDate = FOREVER.equals(end) ?
                    rawPlainDateFormat.parse(foreverString) : rawPlainDateFormat.parse(end);
            String targetBeginDateString = newDateFormat.format(beginDate);
            String targetEndDateString = newDateFormat.format(endDate);
            return (String[]) Arrays.asList(targetBeginDateString, targetEndDateString).toArray();
        } catch (ParseException e) {
            throw new YPLException(YPLConstant.ERROR_PARAMETER_WRONG, "解析身份证有效期失败");
        }
    }
    public static String getBeginOfValidation(String validation, String dateFormat) {
        return convertIdCardValidation(validation, dateFormat)[0];
    }
    public static String getEndOfValidation(String validation, String dateFormat) {
        return convertIdCardValidation(validation, dateFormat)[1];
    }

    public static String stringOr(String... strings) {
        for (String string : strings) {
            if (!TextUtils.isEmpty(string)) {
                return string;
            }
        }
        return "";
    }

    @NotNull
    public static String getMaskString(@Nullable String origin, int prefixLength, int paddingLength,
                                       int suffixLength, String paddingChar) {
        if (TextUtils.isEmpty(origin)) {
            return ""; // null | "" => ""
        }
        int length = origin.length();
        if (prefixLength + suffixLength > length) {
            return origin; // 非打码范围过大 返回源字符串
        }
        if (prefixLength < 0 || paddingLength < 0 || suffixLength < 0) {
            return origin; // 打码参数不正确 返回源字符串
        }
        String prefix = origin.substring(0, prefixLength);
        int suffixIndexBegin = length - suffixLength;
        String suffix = origin.substring(suffixIndexBegin);
        String padding = String.join("", Collections.nCopies(paddingLength, paddingChar));
        return prefix + padding + suffix;
    }

    @NotNull
    public static String getMaskString(@Nullable String origin, int prefixLength, int paddingLength,
                                    int suffixLength, char paddingChar) {
        String charStr = String.valueOf(paddingChar);
        return getMaskString(origin, prefixLength, paddingLength, suffixLength, charStr);
    }

    @NotNull
    public static String getMaskString(@Nullable String origin, int prefixLength, int paddingLength,
                                    int suffixLength) {
        return getMaskString(origin, prefixLength, paddingLength, suffixLength, "*");
    }

    /**
     * 转化为首字母大写
     * @param name 字符串
     * @return 结果
     */
    @NotNull
    public static String captureName(@NotNull String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }
    public static String spliceNamespace(@NotNull String name, String namespace) {
        if (TextUtils.isEmpty(namespace)) {
            return name;
        }
        return namespace + captureName(name);
    }

    public static void main(String[] args) {
//        System.out.println(ChannelStringUtils.getBeginOfValidation("20190101-20190102", "yyyy-MM-dd"));
//        System.out.println(ChannelStringUtils.getEndOfValidation("20190101-20190102", "yyyy-MMdd"));
//        System.out.println(ChannelStringUtils.getEndOfValidation("20190101-长期", "yyyyMMddHHmmssSSS"));
        System.out.println(ChannelStringUtils.getMaskString("1234567890", 0,
                3,  4));
    }
}
